package com.rybbaby.tss.front.tssMemberPackage.service.imp;

import com.rybbaby.tss.core.web.service.imp.BaseServiceImp;
import com.rybbaby.tss.front.tssMealFee.entity.TssMealFee;
import com.rybbaby.tss.front.tssMealFee.service.ITssMealFeeService;
import com.rybbaby.tss.front.tssMemberPackage.dao.ITssMemberPackageDao;
import com.rybbaby.tss.front.tssMemberPackage.entity.ExportEntity.ErrorMessage;
import com.rybbaby.tss.front.tssMemberPackage.entity.ImportEntity.MemberImport;
import com.rybbaby.tss.front.tssMemberPackage.entity.TssMemberPackage;
import com.rybbaby.tss.front.tssMemberPackage.service.ITssMemberPackageService;
import com.rybbaby.tss.front.tssMemberPackage.service.ImportExcelService;
import com.rybbaby.tss.front.tssPackage.dao.ITssPackageDao;
import com.rybbaby.tss.front.tssPackage.entity.TssPackage;
import com.rybbaby.tss.front.tssPayment.entity.TssPayment;
import com.rybbaby.tss.front.tssPayment.service.ITssPaymentService;
import com.rybbaby.tss.front.tssPaymentHistory.service.ITssPaymentHistoryService;
import com.rybbaby.tss.front.tssPertainRelation.dao.ITssPertainRelationDao;
import com.rybbaby.tss.front.tssPertainRelation.entity.TssPertainRelation;
import com.rybbaby.tss.front.tssRightStatistic.service.ITssRightStatisticService;
import com.rybbaby.tss.front.tssStudent.dao.ITssStudentDao;
import com.rybbaby.tss.front.tssStudent.entity.TssStudent;
import com.rybbaby.tss.platform.sysAdminUser.dao.ISysAdminUserDao;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.Serializable;
import java.sql.Timestamp;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * 会员模块导入excel实现
 * 杨然
 */
@Service
public class ImportExcelServiceImp extends BaseServiceImp<Object> implements ImportExcelService {

    @Autowired
    private ITssStudentDao tssStudentDao;
    @Autowired
    private ITssPertainRelationDao pertainRelationDao;
    @Autowired
    private ISysAdminUserDao staffDao;
    @Autowired
    private ITssPackageDao packageDao;
    @Autowired
    private ITssMemberPackageDao memberPackageDao;
    @Autowired
    private ITssMealFeeService tssMealFeeService;
    @Autowired
    private ITssPaymentService tssPaymentService;
    @Autowired
    private ITssPaymentHistoryService tssPaymentHistoryService;
    @Autowired
    private ITssRightStatisticService tssRightStatisticService;
    @Autowired
    private ITssMemberPackageService tssMemberPackageService;


    @Override
    public StringBuffer importManage(HttpServletRequest request, String adminUserId) throws Exception {
        List<MemberImport> list = null;
        //序列化工具
        DecimalFormat format = new DecimalFormat("#");

        //初始化自定义实体对象
        MemberImport memberMessage = null;

        //存储错误信息
        StringBuffer reg = null;
        ErrorMessage errorMessage = new ErrorMessage();

        //初始化session
        HttpSession session = request.getSession();

        //正则验证
        String regu = "";

        //储存生日
        String birthday = "";

        MultipartHttpServletRequest mReq = (MultipartHttpServletRequest) request;
        MultipartFile file = mReq.getFile("filePath");

        //根据亲子会员页面 or 过渡会员页面选择导入
        /*String memberType = mReq.getParameter("memberType");*/

        if (file.isEmpty()) {
            return new StringBuffer("请选择导入文件");
        }
        String filename = file.getOriginalFilename();

        String s = filename.substring(filename.lastIndexOf("."));
        System.out.println(s);

        //HSSFWorkBook  97~03	xls
        //XSSFWorkBook 	07~		xlsx
        Workbook wb = null;
        if (s.equals(".xls")) {
            wb = new HSSFWorkbook(file.getInputStream());
        } else if (s.equals(".xlsx")) {
            wb = new XSSFWorkbook(file.getInputStream());
        } else {
            return new StringBuffer("文件格式不正确 请确认后导入");
        }

        Sheet sheet = wb.getSheetAt(0);
        int i = 0;
        for (Row row : sheet) {
            int rowNum = row.getRowNum();
            if (rowNum > 1) {
                i++;

                reg = new StringBuffer();
                memberMessage = new MemberImport();
                memberMessage.getStudent().setStuId(i + "");

                /*获取幼儿信息 */
                //幼儿姓名(必填)
                Cell cell1 = row.getCell(0);
                if (cell1 == null || cell1.getCellType() == HSSFCell.CELL_TYPE_BLANK) {
                    reg.append(" 幼儿姓名必填 \n");
                } else {
                    String name = cell1.getStringCellValue();
                    String nameReg = "^([\\u4e00-\\u9fa5]+|([a-zA-Z]+\\s?)+)$";
                    if (name.matches(nameReg)) {
                        memberMessage.getStudent().setStuName(cell1.getStringCellValue());

                    } else {
                        errorMessage.setStudentName("幼儿姓名必填");
                        reg.append(" 幼儿姓名要真实哦 \n");
                    }
                }

                //幼儿昵称
                Cell cell2 = row.getCell(1);
                if (cell2 != null && cell2.getCellType() != HSSFCell.CELL_TYPE_BLANK) {
                    memberMessage.getStudent().setStuNickName(cell2.getStringCellValue());
                }

                //幼儿性别(必填)
                Cell cell3 = row.getCell(2);
                if (cell3 != null && cell3.getCellType() != HSSFCell.CELL_TYPE_BLANK) {
                    memberMessage.getStudent().setSex(cell3.getStringCellValue().equals("男") ? 1 : 2);
                } else {
                    errorMessage.setSex("幼儿性别必填");
                    reg.append(" 幼儿的性别必须填写哦 \n");
                }

                //出生日期(必填)
                Cell cell4 = row.getCell(3);
                if (cell4 != null && cell4.getCellType() != HSSFCell.CELL_TYPE_BLANK) {
                    if (HSSFDateUtil.isCellDateFormatted(cell4)) {
                        Date date = cell4.getDateCellValue();
                        memberMessage.getStudent().setBirthday(date);
                    } else {
                        cell4.setCellType(1);
                        String bir = cell4.getStringCellValue();
                        StringBuffer sb = new StringBuffer(bir);
                        sb.insert(6, "-");
                        sb.insert(4, "-");
                        birthday = sb.substring(0, 4);
                        bir = sb.toString();
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                        Date date = sdf.parse(bir);
                        memberMessage.getStudent().setBirthday(date);
                    }
                } else {
                    errorMessage.setBirthday("幼儿出生日期必填");
                    reg.append(" 幼儿出生日期必须填写哦 \n");
                }

                /*获取家长信息 */
                //妈妈电话(必填)
                Cell cell5 = row.getCell(4);
                if (cell5 == null || cell5.getCellType() == HSSFCell.CELL_TYPE_BLANK) {
                    memberMessage.getParentsMessage().getMotherMessage().setMotherPhone("");
                } else {
                    String nameReg = "^\\d+$";
                    //验证手机格式
                    if (format.format(cell5.getNumericCellValue()).matches(nameReg)) {
                        //处理excel中数值类型数据并放到对象中
                        memberMessage.getParentsMessage().getMotherMessage().setMotherPhone(format.format
                                (cell5.getNumericCellValue()));
                    } else {
                        errorMessage.setMotherPhone("需填写真实手机号");
                        reg.append(" 妈妈手机号要写真实的哦 \n");
                    }
                }

                //妈妈姓名(必填)
                Cell cell6 = row.getCell(5);
                if (cell6 == null || cell6.getCellType() == HSSFCell.CELL_TYPE_BLANK) {
                    memberMessage.getParentsMessage().getMotherMessage().setMotherName(null);
                } else {
                    String nameReg = "^([\\u4e00-\\u9fa5]+|([a-zA-Z]+\\s?)+)$";
                    if (cell6.getStringCellValue().matches(nameReg)) {
                        memberMessage.getParentsMessage().getMotherMessage().setMotherName(cell6.getStringCellValue());
                    } else {
                        errorMessage.setMotherName("需填写真实姓名");
                        reg.append(" 妈妈姓名要真实哦 \n");
                    }
                }

                //妈妈:决策人/监护人
                Cell cell7 = row.getCell(6);
                if (cell7 == null || cell7.getCellType() == HSSFCell.CELL_TYPE_BLANK) {

                } else {
                    memberMessage.getParentsMessage().getMotherMessage().setPositionType1(cell7.getStringCellValue()
                            .equals("决策人") ? 1 : (cell7.getStringCellValue().equals("监护人") ? 2 : null));
                }

                //爸爸电话
                Cell cell8 = row.getCell(7);
                if (cell8 == null || cell8.getCellType() == HSSFCell.CELL_TYPE_BLANK) {
                    memberMessage.getParentsMessage().getFatherMessage().setFatherPhone("");
                } else {
                    String fatherPhoneReg = "^\\d+$";
                    if (format.format(cell8.getNumericCellValue()).matches(fatherPhoneReg)) {
                        memberMessage.getParentsMessage().getFatherMessage().setFatherPhone(format.format
                                (cell8.getNumericCellValue()));
                    } else {
                        errorMessage.setFatherPhone("需填写真实手机号");
                        reg.append(" 爸爸手机号必须真实哦 \n");
                    }
                }

                //爸爸姓名
                Cell cell9 = row.getCell(8);
                if (cell9 == null || cell9.getCellType() == HSSFCell.CELL_TYPE_BLANK) {
                    memberMessage.getParentsMessage().getFatherMessage().setFatherName(null);
                } else {
                    String nameReg = "^([\\u4e00-\\u9fa5]+|([a-zA-Z]+\\s?)+)$";
                    if (cell9.getStringCellValue().matches(nameReg)) {
                        memberMessage.getParentsMessage().getFatherMessage().setFatherName(cell9.getStringCellValue());
                    } else {
                        errorMessage.setFatherName("需填写真实姓名");
                        reg.append(" 爸爸姓名要真实哦 \n");
                    }
                }

                //爸爸:决策人/监护人
                Cell cell10 = row.getCell(9);
                if (cell10 == null || cell10.getCellType() == HSSFCell.CELL_TYPE_BLANK) {
                    memberMessage.getParentsMessage().getFatherMessage().setPositionType2(0);
                } else {
                    memberMessage.getParentsMessage().getFatherMessage().setPositionType2(cell10.getStringCellValue()
                            .equals("决策人") ? 1 : (cell10.getStringCellValue().equals("监护人") ? 2 : null));
                }

                /**
                 * 验证父母信息至少有一条是完整的
                 * */
                if (
                        (
                                memberMessage.getParentsMessage().getMotherMessage().getMotherPhone() != null
                                        && memberMessage.getParentsMessage().getMotherMessage().getMotherName() != null
                                        && memberMessage.getParentsMessage().getMotherMessage().getPositionType1() !=
                                        null
                        ) ||
                                (
                                        memberMessage.getParentsMessage().getFatherMessage().getFatherPhone() != null
                                                && memberMessage.getParentsMessage().getFatherMessage().getFatherName
                                                () != null
                                                && memberMessage.getParentsMessage().getFatherMessage()
                                                .getPositionType2() != null
                                )
                        ) {

                    //母亲父亲数据都完整
                    if (
                            (
                                    memberMessage.getParentsMessage().getMotherMessage().getMotherPhone() != null
                                            && memberMessage.getParentsMessage().getMotherMessage().getMotherName()
                                            != null
                                            && memberMessage.getParentsMessage().getMotherMessage().getPositionType1
                                            () != null
                            ) &&
                                    (
                                            memberMessage.getParentsMessage().getFatherMessage().getFatherPhone() !=
                                                    null
                                                    && memberMessage.getParentsMessage().getFatherMessage()
                                                    .getFatherName() != null
                                                    && memberMessage.getParentsMessage().getFatherMessage()
                                                    .getPositionType2() != null
                                    )
                            ) {
                        memberMessage.getParentsMessage().setAffiliationRelation(3);
                    }

                    //母亲数据完整
                    else if (
                            memberMessage.getParentsMessage().getMotherMessage().getMotherPhone() != null
                                    && memberMessage.getParentsMessage().getMotherMessage().getMotherName() != null
                                    && memberMessage.getParentsMessage().getMotherMessage().getPositionType1() != null
                            ) {
                        memberMessage.getParentsMessage().setAffiliationRelation(2);
                    }

                    //父亲数据完整
                    else if (
                            memberMessage.getParentsMessage().getFatherMessage().getFatherPhone() != null
                                    && memberMessage.getParentsMessage().getFatherMessage().getFatherName() != null
                                    && memberMessage.getParentsMessage().getFatherMessage().getPositionType2() != null
                            ) {
                        memberMessage.getParentsMessage().setAffiliationRelation(1);
                    }
                } else {
                    errorMessage.setPositionType1("父母信息至少完整得填写一条(电话、姓名、是决策人或监护人)");
                    reg.append(" 父母信息至少完整得填写一条(电话、姓名、是决策人或监护人) \n");
                }

                /*合同基本信息*/
                //合同类别(必填 1:亲子 2:过渡)
                Cell cell11 = row.getCell(10);
                if (cell11 == null || cell11.getCellType() == HSSFCell.CELL_TYPE_BLANK) {
                    errorMessage.setContractType("合同类别必须选择其中一项");
                    reg.append(" 合同类别为必填项 \n");
                } else {
                    memberMessage.getContract().setType(cell11.getStringCellValue().equals("亲子") ? 1 : 2);
                }

                //合同编号(必填)
                Cell cell12 = row.getCell(11);
                if (cell12 == null || cell12.getCellType() == HSSFCell.CELL_TYPE_BLANK) {
                    errorMessage.setContractNumber("合同编号必须填写");
                    reg.append(" 合同编号为必填项哦 \n");
                } else {
                    regu = "^[A-Za-z0-9]+$";
                    if (format.format(cell12.getNumericCellValue()).matches(regu)) {
                        memberMessage.getContract().setContractNumber(format.format(cell12.getNumericCellValue()));
                    } else {
                        errorMessage.setContractNumber("需填写真实的合同编号");
                        reg.append(" 合同编号格式不正确 \n");
                    }
                }

                //顾问
                Cell cell13 = row.getCell(12);
                if (cell13 == null || cell13.getCellType() == HSSFCell.CELL_TYPE_BLANK) {

                } else {
                    memberMessage.getContract().setStaffName(cell13.getStringCellValue());
                }

                //合同签订日期(必填)
                Cell cell14 = row.getCell(13);
                if (cell14 == null || cell14.getCellType() == HSSFCell.CELL_TYPE_BLANK) {
                    errorMessage.setCreateTime("合同签订日期必须填写");
                    reg.append(" 合同签订日期为必填项哦 \n");
                } else {
                    cell14.setCellType(1);
                    String bir = cell14.getStringCellValue();
                    StringBuffer sb = new StringBuffer(bir);
                    regu = "^\\d{8}$";

                    if (sb.toString().matches(regu)) {
                        sb.insert(6, "-");
                        sb.insert(4, "-");
                        bir = sb.toString();
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                        Date date = sdf.parse(bir);
                        memberMessage.getContract().setCreateTime(new Timestamp(date.getTime()));
                    } else {
                        errorMessage.setCreateTime("合同签订日期格式不正确");
                        reg.append(" 合同签订日期格式不正确 \n");
                    }
                }

                //失效日期(必填)
                Cell cell15 = row.getCell(14);
                if (cell15 == null || cell15.getCellType() == HSSFCell.CELL_TYPE_BLANK) {
                    errorMessage.setDueTime("失效日期为必填项");
                    reg.append(" 失效日期为必填项哦 \n");
                } else {
                    cell15.setCellType(1);
                    String bir = cell15.getStringCellValue();
                    StringBuffer sb = new StringBuffer(bir);
                    regu = "^\\d{8}$";

                    if (sb.toString().matches(regu)) {
                        sb.insert(6, "-");
                        sb.insert(4, "-");
                        bir = sb.toString();
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                        Date date = sdf.parse(bir);
                        memberMessage.getContract().setDueTime(new Timestamp(date.getTime()));
                    } else {
                        errorMessage.setDueTime("失效日期格式不正确");
                        reg.append(" 合同失效日期格式不正确 \n");
                    }

                    //合同总金额
                    Cell cell16 = row.getCell(15);
                    if (cell16 == null || cell16.getCellType() == HSSFCell.CELL_TYPE_BLANK) {
                        errorMessage.setTheTotalPriceAfterTheClass("合同总金额必须填写");
                    } else {
                        regu = "^\\d{1,5}+(\\.\\d{1,2})?$";
                        if ((cell16.getNumericCellValue() + "").matches(regu)) {
                            memberMessage.getContract().setTheTotalPriceAfterTheClass(cell16.getNumericCellValue());
                        } else {
                            errorMessage.setTheTotalPriceAfterTheClass("合同总金额输入不正确");
                            reg.append(" 请检查合同总金额是否输入正确 \n");
                        }
                    }
                }

                //判断亲子or过渡(根据填写数据判断并解析)
                if (memberMessage.getContract().getType() != null && memberMessage.getContract().getType() == 1) {

                    //报名费
                    Cell cell17 = row.getCell(16);
                    if (cell17 == null || cell17.getCellType() == HSSFCell.CELL_TYPE_BLANK) {

                    } else {
                        regu = "^\\d{1,4}+(\\.\\d{1,2})?$";
                        if ((cell17.getNumericCellValue() + "").matches(regu)) {
                            memberMessage.getContract().setAmount(cell17.getNumericCellValue());
                        } else {
                            reg.append(" 请检查报名费是否输入正确 \n");
                        }
                    }

                    /*亲子合同*/
                    //课时包折后总价
                    Cell cell18 = row.getCell(17);
                    if (cell18 == null || cell18.getCellType() == HSSFCell.CELL_TYPE_BLANK) {
                        errorMessage.setSumPriceP("折后总价必须填写");
                    } else {
                        regu = "^\\d{1,4}+(\\.\\d{1,2})?$";
                        if (format.format(cell18.getNumericCellValue()).matches(regu)) {
                            memberMessage.getContract().getParentContract().setSumPrice
                                    (cell18.getNumericCellValue());
                        } else {
                            reg.append(" 请检查折后总价是否输入正确 \n");
                        }
                    }

                    //课时包名称(必填)
                    Cell cell19 = row.getCell(18);
                    if (cell19 == null || cell19.getCellType() == HSSFCell.CELL_TYPE_BLANK) {
                        errorMessage.setMemberPackageNameP("课时包名称必须填写");
                    } else {
                        memberMessage.getContract().getParentContract().setMemberPackageName
                                (cell19.getStringCellValue());
                    }

                    //赠送课时(必填)
                    Cell cell20 = row.getCell(19);
                    if (cell20 == null || cell20.getCellType() == HSSFCell.CELL_TYPE_BLANK) {
                        errorMessage.setPresentClassHourP("赠送课时必须填写");
                    } else {
                        regu = "^\\d{1,}$";
                        if (format.format(cell20.getNumericCellValue()).matches(regu)) {
                            memberMessage.getContract().getParentContract().setPresentClassHour(Integer.parseInt
                                    (format.format(cell20.getNumericCellValue())));
                        } else {
                            reg.append(" 请检查赠送课时是否输入正确(只能输入整数) \n");
                        }
                    }

                    //赠品价值
                    Cell cell21 = row.getCell(20);
                    if (cell21 == null || cell21.getCellType() == HSSFCell.CELL_TYPE_BLANK) {

                    } else {
                        regu = "^\\d{1,4}+(\\.\\d{1,2})?$";
                        if ((cell21.getNumericCellValue() + "").matches(regu)) {
                            memberMessage.getContract().getParentContract().setGifts(cell21.getNumericCellValue());
                        } else {
                            reg.append(" 请检查赠品价值是否输入正确 \n");
                        }
                    }

                    //试听老师
                    Cell cell22 = row.getCell(21);
                    if (cell22 == null || cell22.getCellType() == HSSFCell.CELL_TYPE_BLANK) {

                    } else {
                        regu = "^([\\u4e00-\\u9fa5]+|([a-zA-Z]+\\s?)+)$";
                        if (cell22.getStringCellValue().matches(regu)) {
                            memberMessage.getContract().getParentContract().setStaffName(cell22.getStringCellValue());

                        } else {
                            reg.append(" 亲子试听老师的名字格式有误 \n");
                        }
                    }

                    /*亲子剩余*/
                    //剩余可选班正课
                    Cell cell23 = row.getCell(22);
                    if (cell23 == null || cell23.getCellType() == HSSFCell.CELL_TYPE_BLANK) {

                    } else {
                        regu = "^\\d{1,}$";
                        if (format.format(cell23.getNumericCellValue()).matches(regu)) {
                            memberMessage.getContract().getParentContract().getParentSurplus()
                                    .setSurplusFormalClassHour(Integer.parseInt(format.format
                                            (cell23.getNumericCellValue())));
                        } else {
                            reg.append(" 请检查赠送课时是否输入正确(只能输入整数) \n");
                        }
                    }

                    //剩余赠课时
                    Cell cell24 = row.getCell(23);
                    if (cell24 == null || cell24.getCellType() == HSSFCell.CELL_TYPE_BLANK) {

                    } else {
                        regu = "^\\d{1,}$";
                        if (format.format(cell24.getNumericCellValue()).matches(regu)) {
                            memberMessage.getContract().getParentContract().getParentSurplus()
                                    .setSurplusPresentClassHour(Integer.parseInt(format.format
                                            (cell24.getNumericCellValue())));
                        } else {
                            reg.append(" 请检查剩余赠送课时是否输入正确(只能输入整数) \n");
                        }
                    }

                    //补课
                    Cell cell25 = row.getCell(24);
                    if (cell25 == null || cell25.getCellType() == HSSFCell.CELL_TYPE_BLANK) {

                    } else {
                        regu = "^\\d{1,}$";
                        if (format.format(cell25.getNumericCellValue()).matches(regu)) {
                            memberMessage.getContract().getParentContract().getParentSurplus()
                                    .setRemediationClassHour
                                            (Integer.parseInt(format.format(cell25.getNumericCellValue())));
                        } else {
                            reg.append(" 请检查补课是否输入正确(填写补课课时哦) \n");
                        }
                    }

                    /*备注*/
                    Cell cell26 = row.getCell(25);
                    if (cell26 == null || cell26.getCellType() == HSSFCell.CELL_TYPE_BLANK) {

                    } else {
                        try {
                            memberMessage.setRemark(cell26.getStringCellValue());
                        } catch (Exception e){
                            errorMessage.setRemark("备注信息不能纯数字");
                        }
                    }

                } else if (memberMessage.getContract().getType() != null && memberMessage.getContract().getType() ==
                        2) {

                    /*验证过渡信息*/
                    //课时包折后总价(应支付总额)
                    Cell cell17 = row.getCell(16);
                    if (cell17 == null || cell17.getCellType() == HSSFCell.CELL_TYPE_BLANK) {
                        errorMessage.setSumPriceT("课时包折后总价需要填写");
                        reg.append(" 课时包折后总价必填哦 \n");
                    } else {
                        regu = "^\\d{1,5}+(\\.\\d{1,2})?$";
                        if ((cell17.getNumericCellValue() + "").matches(regu)) {
                            memberMessage.getContract().getTransitionContract().setSumPrice
                                    (cell17.getNumericCellValue());
                        } else {
                            errorMessage.setSumPriceT("课时包折后总价输入不正确");
                            reg.append(" 请检查课时包折后总价是否输入正确 \n");
                        }
                    }

                    //一次性物品费
                    Cell cell18 = row.getCell(17);
                    if (cell18 == null || cell18.getCellType() == HSSFCell.CELL_TYPE_BLANK) {
                        errorMessage.setAmount2T("一次性物品费用必填");
                        reg.append(" 一次性物品费用必填哦 \n");
                    } else {
                        regu = "^\\d{1,4}+(\\.\\d{1,2})?$";
                        if ((cell18.getNumericCellValue() + "").matches(regu)) {
                            memberMessage.getContract().getTransitionContract().setAmount2
                                    (cell18.getNumericCellValue());
                        } else {
                            errorMessage.setAmount2T("请检查一次性物品费是否输入正确");
                            reg.append(" 请检查一次性物品费是否输入正确 \n");
                        }
                    }

                    //日餐点费(必填)
                    Cell cell19 = row.getCell(18);
                    if (cell19 == null || cell19.getCellType() == HSSFCell.CELL_TYPE_BLANK) {
                        errorMessage.setAmount3T("日餐点费必须填写哦");
                        reg.append(" 日餐点费必须填写哦 \n");
                    } else {
                        regu = "^\\d{1,4}+(\\.\\d{1,2})?$";
                        if (format.format(cell19.getNumericCellValue()).matches(regu)) {
                            memberMessage.getContract().getTransitionContract().setAmount3(cell19.getNumericCellValue
                                    ());
                        } else {
                            errorMessage.setAmount3T("请检查日餐点费是否输入正确");
                            reg.append(" 请检查日餐点费是否输入正确 \n");
                        }
                    }

                    //课程包性质(必填)
                    Cell cell20 = row.getCell(19);
                    if (cell20 == null || cell20.getCellType() == HSSFCell.CELL_TYPE_BLANK) {
                        errorMessage.setNatureT("课程包性质必填 全日/半日");
                        reg.append(" 课程包性质必填 全日/半日 \n");
                    } else {
                        memberMessage.getContract().getTransitionContract().setNature(cell20.getStringCellValue()
                                .equalsIgnoreCase("全日") ? 1 : cell20.getStringCellValue().equalsIgnoreCase("半日") ? 2
                                : null);
                    }

                    //课时包名称(必填)
                    Cell cell21 = row.getCell(20);
                    if (cell21 == null || cell21.getCellType() == HSSFCell.CELL_TYPE_BLANK) {
                        errorMessage.setMemberPackageNameT("课时包名称必填");
                        reg.append(" 过渡会员中课时包名称填写有误 \n");
                    } else {
                        memberMessage.getContract().getTransitionContract().setPackageName(cell21.getStringCellValue());
                    }

                    //赠送总天/次数(必填)
                    Cell cell22 = row.getCell(21);
                    if (cell22 == null || cell22.getCellType() == HSSFCell.CELL_TYPE_BLANK) {
                        errorMessage.setGiftAttendanceDayT("总天/次数为必填项 需要填写整数");
                        reg.append(" 总天/次数为必填项哦 \n");
                    } else {
                        regu = "^\\d{1,}$";
                        if (format.format(cell22.getNumericCellValue()).matches(regu)) {
                            memberMessage.getContract().getTransitionContract().setGiftAttendanceDay((Integer.parseInt
                                    (format.format(cell22.getNumericCellValue()))));
                        } else {
                            errorMessage.setGiftAttendanceDayT("总天/次数需要填写整数");
                            reg.append(" 总天/次数只能填写整数哦 \n");
                        }
                    }

                    //赠送次数是否收材料费 (0:不赠送,否 1赠送,是) (必填)
                    Cell cell23 = row.getCell(22);
                    if (cell23 == null || cell23.getCellType() == HSSFCell.CELL_TYPE_BLANK) {
                        errorMessage.setIsComplimentaryMealsT("赠送次数是否收材料费为必填");
                        reg.append(" 赠送次数是否收材料费为必填项哦 \n");
                    } else {
                        memberMessage.getContract().getTransitionContract().setIsComplimentaryMeals
                                (cell23.getStringCellValue
                                        ().equals("是") ? 1 : 0);
                    }

                    //赠品价值
                    Cell cell24 = row.getCell(23);
                    if (cell24 == null || cell24.getCellType() == HSSFCell.CELL_TYPE_BLANK) {

                    } else {
                        regu = "^\\d{1,4}+(\\.\\d{1,2})?$";
                        if ((cell24.getNumericCellValue() + "").matches(regu)) {
                            memberMessage.getContract().getTransitionContract().setGifts(cell24.getNumericCellValue());
                        } else {
                            reg.append(" 请检查过渡合同中赠品价值是否输入正确 \n");
                        }
                    }

                    //试听老师
                    Cell cell25 = row.getCell(24);
                    if (cell25 == null || cell25.getCellType() == HSSFCell.CELL_TYPE_BLANK) {

                    } else {
                        regu = "^([\\u4e00-\\u9fa5]+|([a-zA-Z]+\\s?)+)$";
                        if (cell25.getStringCellValue().matches(regu)) {
                            memberMessage.getContract().getTransitionContract().setStaffName
                                    (cell25.getStringCellValue());
                        } else {
                            reg.append(" 过渡合同中试听老师名称填写有误(填写老师名字) \n");
                        }
                    }


                    /*过渡剩余*/
                    //剩余总天数(正课)
                    Cell cell26 = row.getCell(25);
                    if (cell26 == null || cell26.getCellType() == HSSFCell.CELL_TYPE_BLANK) {

                    } else {
                        regu = "^\\d{1,}$";
                        if (format.format(cell26.getNumericCellValue()).matches(regu)) {
                            memberMessage.getContract().getTransitionContract().getTransitionSurplus()
                                    .setSurplusPresentClassHour((Integer.parseInt(format.format
                                            (cell26.getNumericCellValue())
                                    )));
                        } else {
                            reg.append(" 过渡剩余总天数填写有误(需整天数) \n");
                        }
                    }

                    //剩余赠课(课次)
                    Cell cell27 = row.getCell(26);
                    if (cell27 == null || cell27.getCellType() == HSSFCell.CELL_TYPE_BLANK) {

                    } else {
                        regu = "^\\d{1,}$";
                        if (format.format(cell27.getNumericCellValue()).matches(regu)) {
                            memberMessage.getContract().getTransitionContract().getTransitionSurplus()
                                    .setSurplusPresentAttendanceDay((Integer.parseInt(format.format
                                            (cell27.getNumericCellValue()))));
                        } else {
                            reg.append(" 剩余赠送课次填写有误(需课次数) \n");
                        }
                    }

                    /*备注*/
                    Cell cell28 = row.getCell(27);
                    if (cell28 == null || cell28.getCellType() == HSSFCell.CELL_TYPE_BLANK) {

                    } else {
                        memberMessage.setRemark(cell28.getStringCellValue());
                    }
                }

                if (list == null) {
                    list = new ArrayList();
                }
                list.add(memberMessage);

            }

        }

        System.out.println(list);
        System.out.println(reg);


        //初始化会员对象
        TssStudent tssStudent = null;
        //初始化家长对象
        TssPertainRelation patriarch1 = null;
        //存储父亲信息对象
        TssPertainRelation patriarch2 = null;


        for (MemberImport memberImport : list) {

            //验证幼儿信息是否重复
            /*String stuName = memberImport.getStudent().getStuName();
            memberImport.getStudent().getStuNickName();
            Integer sex = memberImport.getStudent().getSex();
            tssStudentDao.findBycontractPhone()*/

            //验证合同信息是否重复
            String contractNumber = memberImport.getContract().getContractNumber();
            Integer type = memberImport.getContract().getType();
            List<TssMemberPackage> countByContract = memberPackageDao.findCountByContract(contractNumber);
            if (countByContract != null) {
                errorMessage.setContractNumber("编号为(" + contractNumber + ")的合同已经上传过");
                reg.append(" 编号为(" + contractNumber + ")的合同已经上传过了 \n");
            }

            //亲子 验证赠送课时 必须大于 剩余赠课 (课时)
            //赠送课时
            Integer presentClassHour = memberImport.getContract().getParentContract().getPresentClassHour();
            //剩余赠课
            Integer surplusPresentClassHour = memberImport.getContract().getParentContract().getParentSurplus()
                    .getSurplusPresentClassHour();

            if (surplusPresentClassHour != null && presentClassHour != null) {

                if (presentClassHour < surplusPresentClassHour) {
                    errorMessage.setPresentClassHourP("剩余赠课不能高于赠送课时");
                    reg.append("剩余赠课不能高于赠送天数");
                }

            }

            //过渡 赠送天数 必须大于 剩余赠课
            Integer giftAttendanceDay = memberImport.getContract().getTransitionContract().getGiftAttendanceDay();
            Integer surplusPresentAttendanceDay = memberImport.getContract().getTransitionContract()
                    .getTransitionSurplus().getSurplusPresentAttendanceDay();
            if (giftAttendanceDay != null && surplusPresentAttendanceDay != null) {

                if (giftAttendanceDay < surplusPresentAttendanceDay) {
                    errorMessage.setPresentClassHourP("剩余赠课不能高于赠送天数");
                    reg.append("剩余赠课不能高于赠送天数");
                }

            }

            session.setAttribute("errorMessage", errorMessage);

            if (!"".equals(reg.toString()))
                return reg;

        }

        /**
         * 遍历解析到的数据 分别封装到具体对象 完成导入
         * 最后修改时间 2017年11月27日 19:12:35
         */
        for (MemberImport memberImport : list) {
            tssStudent = new TssStudent();
            patriarch1 = new TssPertainRelation();
            patriarch2 = new TssPertainRelation();
            String stuId = "";
            Integer affiliationRelation = memberImport.getParentsMessage().getAffiliationRelation();
            String motherName = memberImport.getParentsMessage().getMotherMessage().getMotherName();
            String motherPhone = memberImport.getParentsMessage().getMotherMessage().getMotherPhone();
            Integer positionType1 = memberImport.getParentsMessage().getMotherMessage().getPositionType1();
            String fatherName = memberImport.getParentsMessage().getFatherMessage().getFatherName();
            String fatherPhone = memberImport.getParentsMessage().getFatherMessage().getFatherPhone();
            Integer positionType2 = memberImport.getParentsMessage().getFatherMessage().getPositionType2();

            if (memberImport.getStudent().getStuName() != null)
                tssStudent.setName(memberImport.getStudent().getStuName());

            if (memberImport.getStudent().getStuNickName() != null)
                tssStudent.setNickName(memberImport.getStudent().getStuNickName());

            if (memberImport.getStudent().getSex() != null)
                tssStudent.setSex(memberImport.getStudent().getSex());

            if (memberImport.getStudent().getBirthday() != null)
                tssStudent.setBirthdayDate(memberImport.getStudent().getBirthday());

            //将创建时间放到幼儿对象中
            tssStudent.setCreateTime(new Timestamp(System.currentTimeMillis()));
            //修改时间
            tssStudent.setUpdateTime(new Timestamp(System.currentTimeMillis()));
            //将决策人电话放到幼儿对象中
            Integer positionType11 = memberImport.getParentsMessage().getMotherMessage().getPositionType1();
            if (positionType11 != null && positionType11 == 1) {
                tssStudent.setContactPhone(motherPhone);
            }
            Integer positionType21 = memberImport.getParentsMessage().getFatherMessage().getPositionType2();
            if (positionType21 != null && positionType21 == 1) {
                tssStudent.setContactPhone(fatherPhone);
            }
            //将顾问id放到幼儿对象中
            String staffId = staffDao.findStaffIdByName(memberImport.getContract().getStaffName());
            tssStudent.setEmpAdminUserId(staffId);
            //操作者id
            tssStudent.setAdminUserId(adminUserId);
            //年龄
            Timestamp timestamp = new Timestamp(System.currentTimeMillis());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
            String currentTime = sdf.format(new Date());
            Double i1 = Double.parseDouble(currentTime) - Double.parseDouble(birthday);
            tssStudent.setAge(i1);
            //是否会员
            tssStudent.setIsMember(true);

            //添加会员
            String stuName = memberImport.getStudent().getStuName();
            String stuNickName = memberImport.getStudent().getStuNickName();
            Integer sex = memberImport.getStudent().getSex();
            List<TssStudent> tssStudents = tssStudentDao.validationStudent(stuName, stuNickName, sex);
            if (tssStudents == null) {
                stuId = tssStudentDao.saveInport(tssStudent);
            } else {
                errorMessage.setStudentName(memberImport.getStudent().getStuName() + "已经添加过了");
            }

            if (affiliationRelation == 1) {
                patriarch1.setStudentId(stuId);
                patriarch1.setPatriarchName(fatherName);
                patriarch1.setContactPhone(fatherPhone);
                patriarch1.setPositionType(positionType2);
                patriarch1.setAffiliationRelation(1);
                patriarch1.setCreateTime(new Timestamp(System.currentTimeMillis()));

                if (pertainRelationDao.validationParentsMessage(fatherName, fatherPhone) == null)
                    pertainRelationDao.saveImport(patriarch1);
            }

            if (affiliationRelation == 2) {
                patriarch2.setStudentId(stuId);
                patriarch2.setPatriarchName(motherName);
                patriarch2.setContactPhone(motherPhone);
                patriarch2.setPositionType(positionType1);
                patriarch2.setAffiliationRelation(2);
                patriarch2.setCreateTime(new Timestamp(System.currentTimeMillis()));

                if (pertainRelationDao.validationParentsMessage(motherName, motherPhone) == null)
                    pertainRelationDao.saveImport(patriarch2);
            }

            if (affiliationRelation == 3) {
                patriarch1.setStudentId(stuId);
                patriarch1.setPatriarchName(fatherName);
                patriarch1.setContactPhone(fatherPhone);
                patriarch1.setPositionType(positionType2);
                patriarch1.setAffiliationRelation(1);
                patriarch1.setCreateTime(new Timestamp(System.currentTimeMillis()));

                if (pertainRelationDao.validationParentsMessage(fatherName, fatherPhone) == null)
                    pertainRelationDao.saveImport(patriarch1);

                patriarch2.setStudentId(stuId);
                patriarch2.setPatriarchName(motherName);
                patriarch2.setContactPhone(motherPhone);
                patriarch2.setPositionType(positionType1);
                patriarch2.setAffiliationRelation(2);
                patriarch2.setCreateTime(new Timestamp(System.currentTimeMillis()));

                if (pertainRelationDao.validationParentsMessage(motherName, motherPhone) == null)
                    pertainRelationDao.saveImport(patriarch2);
            }

            TssPayment paymentP = null;
            TssPayment paymentT = null;

            TssMemberPackage memberPackageP = null;
            TssMemberPackage memberPackageT = null;
            TssMealFee tssMealFee = null;

            //导入亲子
            if (memberImport.getContract().getType() == 1) {
                paymentP = new TssPayment();
                memberPackageP = new TssMemberPackage();
                String memberPackageName = memberImport.getContract().getParentContract().getMemberPackageName();
                TssPackage packageMessage = packageDao.findPackageByPackageName(memberPackageName);

                /*课时包合同基本信息*/
                //会员id
                memberPackageP.setMemberId(stuId);

                //会员状态 1亲子 2过渡
                memberPackageP.setType(1);

                //合同编号
                memberPackageP.setContractNumber(memberImport.getContract().getContractNumber());

                //顾问
                if (adminUserId != null) {
                    memberPackageP.setEmpAdminUserId(staffDao.findStaffIdByName(memberImport.getContract().getStaffName()));
                }

                //报名状态 0新报 1续报
                //System.out.println(stuId);
                Integer type = memberImport.getContract().getType();
                Integer studentCount = memberPackageDao.getCount(stuId, type);
                if (studentCount > 0) {
                    memberPackageP.setApplyStatus(1);
                } else {
                    memberPackageP.setApplyStatus(0);
                }

                //到期时间
                memberPackageP.setDueTime(memberImport.getContract().getDueTime());

                //赠品价值
                memberPackageP.setGifts(memberImport.getContract().getParentContract().getGifts());

                //试听老师
                memberPackageP.setTeacherId(staffDao.findStaffIdByName(memberImport.getContract().getParentContract()
                        .getStaffName()));

                //折后单价(课时包现单价) = 折后总价(sumPrice)/(亲子总课时数)
                Double sumPrice1 = memberImport.getContract().getParentContract().getSumPrice();
                Integer totalClassHour1 = packageMessage.getTotalClassHour();
                totalClassHour1 = totalClassHour1 == null ? 1 : totalClassHour1;
                DecimalFormat df = new DecimalFormat("#.00");
                if (sumPrice1 != null && totalClassHour1 != null) {
                    Double nowUnitPrice = sumPrice1 / totalClassHour1;
                    memberPackageP.setNowUnitPrice(Double.parseDouble(df.format(nowUnitPrice)));
                }

                //应支付总价(sumPrice)
                memberPackageP.setSumPrice(sumPrice1);

                //优惠金额
                memberPackageP.setDiscountedPrice(0.0);

                //课时包名称
                memberPackageName = memberImport.getContract().getParentContract().getMemberPackageName();
                packageMessage = packageDao.findPackageByPackageName(memberPackageName);
                if (packageMessage.getName() != null) {
                    memberPackageP.setPackageId(packageMessage.getId());
                } else {
                    return reg.append(" 课时包名称填写不正确 \n");
                }

                //可用课时数(剩余正课时 + 剩余赠课时)usableTotalClassHour
                Integer surplusFormalClassHour3 = memberImport.getContract().getParentContract()
                        .getParentSurplus().getSurplusFormalClassHour();
                Integer surplusPresentClassHour2 = memberImport.getContract()
                        .getParentContract().getParentSurplus().getSurplusPresentClassHour();
                if (surplusFormalClassHour3 != null && surplusPresentClassHour2 != null) {
                    memberPackageP.setUsableTotalClassHour(surplusFormalClassHour3 + surplusPresentClassHour2);
                } else {
                    memberPackageP.setUsableTotalClassHour(0);
                }

                //剩余课时数(剩余正课时 + 剩余赠课时)surplusClassHour
                Integer surplusFormalClassHour4 = memberImport.getContract().getParentContract()
                        .getParentSurplus().getSurplusFormalClassHour();
                surplusFormalClassHour4 = surplusFormalClassHour4 == null ? 0 : surplusFormalClassHour4;
                Integer surplusPresentClassHour3 = memberImport.getContract()
                        .getParentContract().getParentSurplus().getSurplusPresentClassHour();
                surplusPresentClassHour3 = surplusPresentClassHour3 == null ? 0 : surplusPresentClassHour3;

                    memberPackageP.setSurplusClassHour(surplusFormalClassHour4 + surplusPresentClassHour3);

                //剩余正课时surplusFormalClassHour
                Integer surplusFormalClassHour1 = memberImport.getContract().getParentContract()
                        .getParentSurplus().getSurplusFormalClassHour();
                memberPackageP.setSurplusFormalClassHour(surplusFormalClassHour1);

                //已消耗课时数0 consumptionClassHour
                memberPackageP.setConsumptionClassHour(0);

                //已消耗课时数(赠)0   giftConsumptionClassHour
                memberPackageP.setGiftConsumptionClassHour(0);

                //未消耗课时数0 unusedClassHour
                memberPackageP.setUnusedClassHour(0);

                //未消耗课时数(正)0 positiveUnusedClassHour
                memberPackageP.setPositiveUnusedClassHour(0);

                //未消耗课时数(赠)0 giftUnusedClassHour
                memberPackageP.setGiftUnusedClassHour(0);

                //剩余有效时期365 surplusValidDate
                memberPackageP.setSurplusValidDate(365);

                //当前操作者Id 0
                memberPackageP.setAdminUserId(adminUserId);

                //备注
                memberPackageP.setRemark(memberImport.getRemark());

                //状态(1、 使用中 2、 待激活 3、注销) packageStatus 1
                memberPackageP.setPackageStatus(1);

                //创建时间
                Timestamp createTime = memberImport.getContract().getCreateTime();
                memberPackageP.setCreateTime(createTime);

                //修改时间
                memberPackageP.setUpdateTime(new Timestamp(System.currentTimeMillis()));

                //补课
                memberPackageP.setRemediationClassHour(memberImport.getContract().getParentContract().getParentSurplus()
                        .getRemediationClassHour());

                //剩余可选班(正课)
                Integer surplusFormalClassHour2 = memberImport.getContract().getParentContract().getParentSurplus()
                        .getSurplusFormalClassHour();

                //剩余赠课时
                Integer surplusPresentClassHour = memberImport.getContract().getParentContract().getParentSurplus()
                        .getSurplusPresentClassHour();

                //补课
                Integer remediationClassHour = memberImport.getContract().getParentContract().getParentSurplus()
                        .getRemediationClassHour();

                //赠送课时 必填
                Integer presentClassHour = memberImport.getContract().getParentContract().getPresentClassHour();

                Integer totalClassHour = packageMessage.getTotalClassHour();
                totalClassHour = totalClassHour == null ? 0 : totalClassHour;
                surplusFormalClassHour2 = surplusFormalClassHour2 == null ? 0 : surplusFormalClassHour2;
                surplusPresentClassHour = surplusPresentClassHour == null ? 0 :surplusPresentClassHour;

                if (surplusFormalClassHour2 != null && surplusPresentClassHour == null && remediationClassHour ==
                        null) {
                    //可用课时数
                    memberPackageP.setUsableTotalClassHour(totalClassHour + presentClassHour);
                    //剩余天数 surplusClassHour
                    memberPackageP.setSurplusClassHour(surplusFormalClassHour2);
                    //剩余正 surplusFormalClassHour
                    memberPackageP.setSurplusFormalClassHour(surplusFormalClassHour2);
                    memberPackageP.setPresentClassHour(presentClassHour);
                } else if (surplusFormalClassHour2 != null && surplusPresentClassHour != null && remediationClassHour
                        == null) {
                    //可用课时数,99+presentClassHour
                    int i2 = surplusFormalClassHour2 + surplusPresentClassHour;
                    memberPackageP.setUsableTotalClassHour(totalClassHour + presentClassHour);
                    //剩余天数 surplusClassHour
                    memberPackageP.setSurplusClassHour(i2);
                    //剩余正 surplusFormalClassHour
                    memberPackageP.setSurplusFormalClassHour(surplusFormalClassHour2);
                    //剩余赠课时 surplusPresentClassHour
                    memberPackageP.setSurplusPresentClassHour(surplusPresentClassHour);
                    //赠送课时数 presentClassHour
                    memberPackageP.setPresentClassHour(presentClassHour);
                } else if (surplusFormalClassHour2 != null && surplusPresentClassHour != null && remediationClassHour
                        != null) {
                    //可出勤天数 usableTotalClassHour
                    int i2 = surplusFormalClassHour2 + surplusPresentClassHour;
                    memberPackageP.setUsableTotalClassHour(totalClassHour + presentClassHour);
                    //剩余天数 surplusClassHour
                    memberPackageP.setSurplusClassHour(i2);
                    //剩余正 surplusFormalClassHour
                    memberPackageP.setSurplusFormalClassHour(surplusFormalClassHour2);
                    //剩余赠课时 surplusPresentClassHour
                    memberPackageP.setSurplusPresentClassHour(surplusPresentClassHour);
                    //补课可用课时数 remediationClassHour
                    memberPackageP.setRemediationClassHour(remediationClassHour);
                    //赠送课时数 presentClassHour
                    memberPackageP.setPresentClassHour(presentClassHour);
                } else if (surplusFormalClassHour2 == null && surplusPresentClassHour != null && remediationClassHour
                        == null) {
                    //可出勤 usableTotalClassHour
                    Integer attendanceDay = packageMessage.getAttendanceDay();
                    attendanceDay = attendanceDay == null ? 0 : attendanceDay;
                    int i2 = surplusPresentClassHour + attendanceDay;
                    memberPackageP.setUsableTotalClassHour(totalClassHour + presentClassHour);
                    //剩余天数 surplusClassHour
                    memberPackageP.setSurplusClassHour(surplusFormalClassHour2 + surplusPresentClassHour);
                    //剩余正 surplusFormalClassHour
                    memberPackageP.setSurplusFormalClassHour(0);
                    //剩余赠课时 surplusPresentClassHour
                    memberPackageP.setSurplusPresentClassHour(surplusPresentClassHour);
                    //赠送课时数 presentClassHour
                    memberPackageP.setPresentClassHour(presentClassHour);
                    //已选班 selectedClassHour
                    memberPackageP.setSelectedClassHour(99);
                    //已消耗课时 consumptionClassHour
                    memberPackageP.setConsumptionClassHour(99);
                    //已消耗课时数(正) positiveConsumptionClassHour
                    memberPackageP.setPositiveConsumptionClassHour(99);
                } else if (surplusFormalClassHour2 == null && surplusPresentClassHour != null && remediationClassHour
                        != null) {
                    //可出勤 usableTotalClassHour
                    Integer attendanceDay = packageMessage.getAttendanceDay();
                    attendanceDay = attendanceDay == null ? 0 : attendanceDay;
                    int i2 = surplusPresentClassHour + attendanceDay;
                    memberPackageP.setUsableTotalClassHour(totalClassHour + presentClassHour);
                    //剩余天数 surplusClassHour
                    memberPackageP.setSurplusClassHour(surplusFormalClassHour2 + surplusPresentClassHour);
                    //剩余正 surplusFormalClassHour
                    memberPackageP.setSurplusFormalClassHour(0);
                    //剩余赠课时 surplusPresentClassHour e
                    memberPackageP.setSurplusPresentClassHour(surplusPresentClassHour);
                    //赠送课时数 presentClassHour
                    memberPackageP.setPresentClassHour(presentClassHour);
                    //补课可用课时数 remediationClassHour
                    memberPackageP.setRemediationClassHour(remediationClassHour);
                    //已选班 selectedClassHour
                    memberPackageP.setSelectedClassHour(99);
                    //已消耗课时 consumptionClassHour
                    memberPackageP.setConsumptionClassHour(99);
                    //已消耗课时数(正) positiveConsumptionClassHour
                    memberPackageP.setPositiveConsumptionClassHour(99);
                } else if (surplusFormalClassHour2 == null && surplusPresentClassHour == null && remediationClassHour
                        != null) {
                    //可出勤 usableTotalClassHour
                    Integer attendanceDay = packageMessage.getAttendanceDay();
                    attendanceDay = attendanceDay == null ? 0 : attendanceDay;
                    surplusPresentClassHour = surplusPresentClassHour == null ? 0 : surplusPresentClassHour;
                    int i2 = surplusPresentClassHour + attendanceDay;
                    memberPackageP.setUsableTotalClassHour(totalClassHour + presentClassHour);
                    //剩余天数 surplusClassHour
                    memberPackageP.setSurplusClassHour(surplusFormalClassHour2 + surplusPresentClassHour);
                    //赠送课时数 presentClassHour
                    memberPackageP.setPresentClassHour(presentClassHour);
                    //剩余正 surplusFormalClassHour
                    memberPackageP.setSurplusFormalClassHour(0);
                    //补课可用课时数 remediationClassHour
                    memberPackageP.setRemediationClassHour(remediationClassHour);
                    //已选班 selectedClassHour
                    memberPackageP.setSelectedClassHour(99);
                    //已消耗课时 consumptionClassHour
                    memberPackageP.setConsumptionClassHour(99);
                    //已消耗课时数(正) positiveConsumptionClassHour
                    memberPackageP.setPositiveConsumptionClassHour(99);
                } else if (surplusFormalClassHour2 == null && surplusPresentClassHour == null && remediationClassHour
                        == null) {
                    //可出勤 usableTotalClassHour
                    memberPackageP.setUsableTotalClassHour(totalClassHour);
                    //剩余天数 surplusClassHour
                    memberPackageP.setSurplusClassHour(packageMessage.getAttendanceDay());
                    //赠送课时数
                    memberPackageP.setPresentClassHour(presentClassHour);
                    //已选班 selectedClassHour
                    memberPackageP.setSelectedClassHour(99);
                    //已消耗课时数(正) positiveConsumptionClassHour
                    memberPackageP.setPositiveConsumptionClassHour(99);
                    //已消耗课时 consumptionClassHour
                    memberPackageP.setConsumptionClassHour(99);
                } else if (surplusFormalClassHour2 == null && surplusPresentClassHour == null && remediationClassHour
                        != null){
                    //可出勤 usableTotalClassHour
                    memberPackageP.setUsableTotalClassHour(totalClassHour);
                    //赠送课时数
                    memberPackageP.setPresentClassHour(0);
                    //剩余正 surplusFormalClassHour
                    memberPackageP.setSurplusFormalClassHour(0);
                    //补课可用课时数 remediationClassHour
                    memberPackageP.setRemediationClassHour(remediationClassHour);
                    //已选班 selectedClassHour
                    memberPackageP.setSelectedClassHour(99);
                    //已消耗课时 consumptionClassHour
                    memberPackageP.setConsumptionClassHour(99);
                    //已消耗课时数(正) positiveConsumptionClassHour
                    memberPackageP.setPositiveConsumptionClassHour(99);
                } else if (surplusFormalClassHour2 != null && surplusPresentClassHour == null && remediationClassHour
                        != null){
                    //可出勤 usableTotalClassHour
                    memberPackageP.setUsableTotalClassHour(totalClassHour);
                    //赠送课时数
                    memberPackageP.setPresentClassHour(presentClassHour);
                    //剩余正 surplusFormalClassHour
                    memberPackageP.setSurplusFormalClassHour(surplusFormalClassHour2);
                    //补课可用课时数 remediationClassHour
                    memberPackageP.setRemediationClassHour(remediationClassHour);
                }

                Serializable saveMemberPackageP = save(memberPackageP);

                //存储课时包缴费信息
                /*缴费表相关*/
                //会员Id
                paymentP.setStuId(stuId);

                //当前操作者Id
                paymentP.setAdminUserId(adminUserId);

                //合同编号
                paymentP.setContractNumber(memberImport.getContract().getContractNumber());

                //会员课时包Id
                paymentP.setPackageId(packageMessage.getId());

                //报名状态 0新报 1续报
                if (studentCount > 0) {
                    paymentP.setApplyStatus(1);
                } else {
                    paymentP.setApplyStatus(0);
                }


                //创建时间
                paymentP.setCreateTime(new Timestamp(System.currentTimeMillis()));

                //修改时间
                paymentP.setUpdateTime(new Timestamp(System.currentTimeMillis()));

                //版本0
                paymentP.setVersion(0);

                //定金状态 1未使用 2 已使用 null
                //paymentP.setDepositStatus(null);

                //缴费金额 amount = 课时包全款
                Double sumPrice = memberImport.getContract().getParentContract().getSumPrice();
                paymentP.setAmount(sumPrice);

                //缴费类型 1课时包全款 2定金3课时包补款 4餐点费5一次性物品费 6其它费用 7报名费
                paymentP.setPaymentType(1);


                Serializable payment = save(paymentP);

                //缴费记录
                TssPayment tssPayment = tssPaymentService.get(payment + "");
                Serializable historyId = tssPaymentHistoryService.saveHistory(tssPayment);
                if (historyId == null) {
                    System.out.println("亲子课时包全款缴费记录保存失败");
                }

                //存储报名费
                if (memberImport.getContract().getAmount() != null) {

                    /*缴费表相关*/
                    paymentP = new TssPayment();
                    //缴费金额 amount = 报名费
                    Double amount = memberImport.getContract().getAmount();
                    paymentP.setAmount(amount);

                    //缴费类型 1课时包全款 2定金3课时包补款 4餐点费5一次性物品费 6其它费用 7报名费
                    paymentP.setPaymentType(7);

                    //会员Id
                    paymentP.setStuId(stuId);

                    //当前操作者Id
                    paymentP.setAdminUserId(adminUserId);

                    //合同编号
                    paymentP.setContractNumber(memberImport.getContract().getContractNumber());

                    //会员课时包Id
                    paymentP.setPackageId(packageMessage.getId());

                    //报名状态 0新报 1续报
                    if (studentCount > 0) {
                        paymentP.setApplyStatus(1);
                    } else {
                        paymentP.setApplyStatus(0);
                    }

                    //创建时间
                    paymentP.setCreateTime(new Timestamp(System.currentTimeMillis()));

                    //修改时间
                    paymentP.setUpdateTime(new Timestamp(System.currentTimeMillis()));

                    //版本0
                    paymentP.setVersion(0);

                    //定金状态 1未使用 2 已使用 null
                    //paymentP.setDepositStatus(null);

                    Serializable payment1 = save(paymentP);

                    //缴费记录
                    TssPayment tssPayment1 = tssPaymentService.get(payment + "");
                    Serializable historyId1 = tssPaymentHistoryService.saveHistory(tssPayment1);
                    if (historyId1 == null) {
                        System.out.println("亲子课时包全款报名费缴费记录保存失败");
                    }

                }

            }
            //导入过渡
            else if (memberImport.getContract().getType() == 2) {
                DecimalFormat df = null;
                memberPackageT = new TssMemberPackage();
                //判断是否收取材料费
                Integer isComplimentaryMeals = memberImport.getContract().getTransitionContract()
                        .getIsComplimentaryMeals();
                //折后总价
                Double sumPrice3 = memberImport.getContract().getTransitionContract().getSumPrice();
                //日餐点费
                Double amount3 = memberImport.getContract().getTransitionContract().getAmount3();
                //赠送天数
                Integer giftAttendanceDay = memberImport.getContract().getTransitionContract().getGiftAttendanceDay();
                //剩余总天数
                Integer surplusPresentClassHour = memberImport.getContract().getTransitionContract()
                        .getTransitionSurplus().getSurplusPresentClassHour();
                //剩余赠课
                Integer surplusPresentAttendanceDay = memberImport.getContract().getTransitionContract()
                        .getTransitionSurplus().getSurplusPresentAttendanceDay();

                //餐点费
                double cdf = 0;

                if (isComplimentaryMeals == 0) {

                    if (surplusPresentClassHour != null){
                        cdf = surplusPresentClassHour * amount3;
                    }
                    memberPackageT.setIsComplimentaryMeals(0);

                } else if (isComplimentaryMeals == 1) {

                    if (surplusPresentClassHour != null && surplusPresentAttendanceDay != null){
                        cdf = (surplusPresentClassHour + surplusPresentAttendanceDay) * amount3;
                    } else if(surplusPresentClassHour == null && surplusPresentAttendanceDay == null){
                        cdf = giftAttendanceDay * amount3;
                    } else if(surplusPresentClassHour != null && surplusPresentAttendanceDay == null){
                        cdf = surplusPresentClassHour * amount3;
                    } else if(surplusPresentClassHour == null && surplusPresentAttendanceDay != null){
                        cdf = surplusPresentAttendanceDay * amount3;
                    }
                    memberPackageT.setIsComplimentaryMeals(1);

                }

                //课时包获取
                String memberPackageName = memberImport.getContract().getTransitionContract().getPackageName();
                TssPackage packageMessage = packageDao.findPackageByPackageName(memberPackageName);

                memberPackageT.setPackageId(packageMessage.getId());
                memberPackageT.setDiscountedPrice(0.0);
                memberPackageT.setSumPrice(sumPrice3);
                memberPackageT.setMemberId(stuId);
                memberPackageT.setAdminUserId(adminUserId);
                memberPackageT.setType(2);
                memberPackageT.setPackageStatus(1);
                if (memberImport.getContract().getTransitionContract().getGiftAttendanceDay() != null) {
                    memberPackageT.setGiftAttendanceDay(memberImport.getContract().getTransitionContract()
                            .getGiftAttendanceDay());
                } else {
                    memberPackageT.setGiftAttendanceDay(packageMessage.getAttendanceDay());
                }

                memberPackageT.setSurplusPresentAttendanceDay(surplusPresentAttendanceDay);
                memberPackageT.setCreateTime(new java.sql.Timestamp(System.currentTimeMillis()));
                memberPackageT.setUpdateTime(new java.sql.Timestamp(System.currentTimeMillis()));
                //dueTime
                memberPackageT.setDueTime(memberImport.getContract().getDueTime());
                //合同编号
                memberPackageT.setContractNumber(memberImport.getContract().getContractNumber());
                //当前操作者id
                memberPackageT.setAdminUserId(adminUserId);

                //报名状态 0新报 1续报
                Integer type = memberImport.getContract().getType();
                Integer studentCount = memberPackageDao.getCount(stuId, type);
                if (studentCount > 0) {
                    memberPackageT.setApplyStatus(1);
                } else {
                    memberPackageT.setApplyStatus(0);
                }

                //课时包现单价 nowUnitPrice 折后总价 / 可出勤天数 (package)
                //折后总价
                Double sumPrice = memberImport.getContract().getTransitionContract().getSumPrice();
                Integer attendanceDay1 = packageMessage.getAttendanceDay();
                attendanceDay1 = attendanceDay1 == null ? 1 : attendanceDay1;
                double v = sumPrice / attendanceDay1;
                memberPackageT.setNowUnitPrice(v);

                //课时包性质 nature 1全日 0半日
                memberPackageT.setNature(memberImport.getContract().getTransitionContract().getNature());

                //顾问
                if (adminUserId != null) {
                    //课时包Id
                    memberPackageT.setEmpAdminUserId(adminUserId);
                }

                //赠品价值
                memberPackageT.setGifts(memberImport.getContract().getTransitionContract().getGifts());

                //试听老师
                memberPackageT.setTeacherId(staffDao.findStaffIdByName(memberImport.getContract()
                        .getTransitionContract()
                        .getStaffName()));

                //备注 remark
                memberPackageT.setRemark(memberImport.getRemark());

                //状态(1、 使用中 2、 待激活 3、注销) packageStatus 1(1)
                memberPackageT.setPackageStatus(1);

                //剩余总天数(正课)
                Integer surplusPresentClassHour2 = memberImport.getContract().getTransitionContract()
                        .getTransitionSurplus().getSurplusPresentClassHour();

                //剩余赠课(课次)
                Integer surplusPresentAttendanceDay1 = memberImport.getContract().getTransitionContract()
                        .getTransitionSurplus().getSurplusPresentAttendanceDay();

                //package
                Integer attendanceDay2 = packageMessage.getAttendanceDay();


                //赠送天数 giftAttendanceDay
                Integer giftAttendanceDay1 = memberImport.getContract().getTransitionContract().getGiftAttendanceDay();

                attendanceDay2 = attendanceDay2 == null ? 0 : attendanceDay2;
                surplusPresentClassHour2 = surplusPresentClassHour2 == null ? 0 : surplusPresentClassHour2;
                surplusPresentAttendanceDay1 = surplusPresentAttendanceDay1 == null ? 0 : surplusPresentAttendanceDay1;

                if (surplusPresentClassHour2 != null && surplusPresentAttendanceDay1 == null) {

                    /*
                    * attendanceDay  可出勤
                    * surplusAttendanceDay 剩余天数
                    * surplusFormalAttendanceDay 剩正
                    * surplusPresentAttendanceDay 剩赠
                    * */
                    //赠送出勤天数 giftAttendanceDay
                    memberPackageT.setGiftAttendanceDay(giftAttendanceDay1);
                    //可出勤天数 attendanceDay
                    memberPackageT.setAttendanceDay(attendanceDay2 + giftAttendanceDay1);
                    //剩余天数 surplusAttendanceDay
                    memberPackageT.setSurplusAttendanceDay(surplusPresentClassHour2 + surplusPresentAttendanceDay1);
                    //剩余正 surplusFormalAttendanceDay
                    memberPackageT.setSurplusFormalAttendanceDay(surplusPresentClassHour2);
                    //剩赠
                    surplusPresentAttendanceDay1 = surplusPresentAttendanceDay1 == null ? 0 :surplusPresentAttendanceDay1;
                    memberPackageT.setSurplusPresentAttendanceDay(surplusPresentAttendanceDay1);
                } else if (surplusPresentClassHour2 != null && surplusPresentAttendanceDay1 != null) {
                    //赠送出勤天数 giftAttendanceDay
                    memberPackageT.setGiftAttendanceDay(giftAttendanceDay1);
                    //可出勤天数 attendanceDay
                    memberPackageT.setAttendanceDay(attendanceDay2 + giftAttendanceDay1);
                    //剩余天数 surplusAttendanceDay
                    memberPackageT.setSurplusAttendanceDay(surplusPresentClassHour2 + surplusPresentAttendanceDay1);
                    //剩余正 surplusFormalAttendanceDay
                    memberPackageT.setSurplusFormalAttendanceDay(surplusPresentClassHour2);
                    //剩赠
                    memberPackageT.setSurplusPresentAttendanceDay(surplusPresentAttendanceDay1);
                } else if (surplusPresentClassHour2 == null && surplusPresentAttendanceDay1 != null) {
                    //赠送出勤天数 giftAttendanceDay
                    memberPackageT.setGiftAttendanceDay(giftAttendanceDay1);
                    //可出勤 attendanceDay
                    memberPackageT.setAttendanceDay(attendanceDay2 + giftAttendanceDay1);
                    //剩余天数 surplusAttendanceDay
                    memberPackageT.setSurplusAttendanceDay(0);
                    //剩余正 surplusFormalAttendanceDay
                    memberPackageT.setSurplusFormalAttendanceDay(0);
                    //剩余赠课时 surplusPresentAttendanceDay
                    memberPackageT.setSurplusPresentAttendanceDay(surplusPresentAttendanceDay1);
                    //已选班 selectedAttendanceDay
                    memberPackageT.setSelectedAttendanceDay(99);
                    //已消耗课时 consumptionAttendanceDay
                    memberPackageT.setConsumptionAttendanceDay(99);
                    //已消耗课时数(正) positiveConsumptionAttendanceDay
                    memberPackageT.setPositiveConsumptionAttendanceDay(99);
                } else if (surplusPresentClassHour2 == null && surplusPresentAttendanceDay1 == null){
                    //赠送出勤天数 giftAttendanceDay
                    memberPackageT.setGiftAttendanceDay(giftAttendanceDay1);
                    //可出勤 attendanceDay
                    memberPackageT.setAttendanceDay(attendanceDay2 + giftAttendanceDay1);
                    //剩余天数 surplusAttendanceDay
                    memberPackageT.setSurplusAttendanceDay(attendanceDay2 + giftAttendanceDay1);
                    //剩余正 surplusFormalAttendanceDay
                    memberPackageT.setSurplusFormalAttendanceDay(attendanceDay2);
                    //剩余赠课时 surplusPresentAttendanceDay
                    memberPackageT.setSurplusPresentAttendanceDay(giftAttendanceDay1);
                }

                //保存memberPackage信息
                Serializable memberPackageSerializable = save(memberPackageT);

                /*缴费信息*/
                //折后总价
                Double sumPrice1 = memberImport.getContract().getTransitionContract().getSumPrice();
                //日餐点费
                Double amount31 = memberImport.getContract().getTransitionContract().getAmount3();
                //一次性物品费
                Double amount2 = memberImport.getContract().getTransitionContract().getAmount2();

                //储存课时包全款
                paymentT = new TssPayment();

                paymentT.setAmount(sumPrice1);

                //会员Id
                paymentT.setStuId(stuId);

                //合同编号
                paymentT.setContractNumber(memberImport.getContract().getContractNumber());

                //会员课时包Id
                paymentT.setPackageId(packageMessage.getId());

                //报名状态 0新报 1续报
                if (studentCount > 0) {
                    paymentT.setApplyStatus(1);
                } else {
                    paymentT.setApplyStatus(0);
                }

                //创建时间
                paymentT.setCreateTime(new Timestamp(System.currentTimeMillis()));

                //修改时间
                paymentT.setUpdateTime(new Timestamp(System.currentTimeMillis()));

                //版本0
                paymentT.setVersion(0);

                //缴费类型 1课时包全款 2定金3课时包补款 4餐点费5一次性物品费 6其它费用 7报名费
                paymentT.setPaymentType(1);

                //操作者Id
                paymentT.setAdminUserId(adminUserId);

                //保存实体
                Serializable save1 = save(paymentT);

                //日餐点费
                if (amount31 != null) {
                    paymentT = new TssPayment();
                    //缴费金额
                    paymentT.setAmount(cdf);

                    //会员Id
                    paymentT.setStuId(stuId);

                    //合同编号
                    paymentT.setContractNumber(memberImport.getContract().getContractNumber());

                    //会员课时包Id
                    paymentT.setPackageId(packageMessage.getId());

                    //报名状态 0新报 1续报
                    if (studentCount > 0) {
                        paymentT.setApplyStatus(1);
                    } else {
                        paymentT.setApplyStatus(0);
                    }

                    //创建时间
                    paymentT.setCreateTime(new Timestamp(System.currentTimeMillis()));

                    //修改时间
                    paymentT.setUpdateTime(new Timestamp(System.currentTimeMillis()));

                    //版本0
                    paymentT.setVersion(0);

                    //缴费类型 1课时包全款 2定金3课时包补款 4餐点费5一次性物品费 6其它费用 7报名费
                    paymentT.setPaymentType(4);

                    //操作者Id
                    paymentT.setAdminUserId(adminUserId);

                    //保存实体
                    Serializable payT = save(paymentT);

                    //缴费记录
                    TssPayment tssPayment = tssPaymentService.get(payT);
                    Serializable historyId = tssPaymentHistoryService.saveHistory(tssPayment);
                    if (historyId == null) {
                        System.out.println("日餐点费存储缴费记录失败");
                    }

                    tssMealFee = new TssMealFee();
                    List<TssMealFee> destEntity = this.tssMealFeeService.findByProperty("stuId",
                            memberPackageSerializable + "");
                    if (destEntity.size() > 0) {
                        cdf += destEntity.get(0).getAmount();
                        tssMealFee.setAmount(cdf);
                        Double surplusAmout = cdf + destEntity.get(0).getSurplusAmout();
                        tssMealFee.setSurplusAmout(surplusAmout);
                        tssMealFee.setUpdateTime(new java.sql.Timestamp(System.currentTimeMillis()));
                        BeanUtils.copyProperties(tssMealFee, destEntity.get(0), "id", "stuId", "dayMealFee",
                                "consumptionAmount", "createTime");
                        this.tssMealFeeService.updateTssMealFee(destEntity.get(0));
                    } else {
                        tssMealFee.setStuId(memberPackageSerializable + "");
                        tssMealFee.setDayMealFee(memberImport.getContract().getTransitionContract().getAmount3());
                        tssMealFee.setAmount(cdf);
                        tssMealFee.setSurplusAmout(cdf);
                        tssMealFee.setConsumptionAmount(cdf);
                        tssMealFee.setCreateTime(new java.sql.Timestamp(System.currentTimeMillis()));
                        tssMealFee.setUpdateTime(new java.sql.Timestamp(System.currentTimeMillis()));

                        Serializable tssMealFeenewId = this.tssMealFeeService.saveTssMealFee(tssMealFee);
                        if (tssMealFeenewId == null) {
                            System.out.println("日餐点费缴费记录保存失败");
                        }
                    }

                }

                //一次性物品费
                if (amount2 != null) {
                    paymentT = new TssPayment();
                    //缴费金额 = amount2
                    paymentT.setAmount(amount2);

                    //会员Id
                    paymentT.setStuId(stuId);

                    //合同编号
                    paymentT.setContractNumber(memberImport.getContract().getContractNumber());

                    //会员课时包Id
                    paymentT.setPackageId(packageMessage.getId());

                    //报名状态 0新报 1续报
                    if (studentCount > 0) {
                        paymentT.setApplyStatus(1);
                    } else {
                        paymentT.setApplyStatus(0);
                    }

                    //创建时间
                    paymentT.setCreateTime(new Timestamp(System.currentTimeMillis()));

                    //修改时间
                    paymentT.setUpdateTime(new Timestamp(System.currentTimeMillis()));

                    //版本0
                    paymentT.setVersion(0);

                    //缴费类型 1课时包全款 2定金3课时包补款 4餐点费5一次性物品费 6其它费用 7报名费
                    paymentT.setPaymentType(5);

                    //操作者Id
                    paymentT.setAdminUserId(adminUserId);

                    //保存实体
                    Serializable payment = save(paymentT);

                    //缴费记录
                    TssPayment tssPayment = tssPaymentService.get(payment);
                    Serializable historyId = tssPaymentHistoryService.saveHistory(tssPayment);
                    if (historyId == null) {
                        System.out.println("一次性物品费存储缴费记录失败");
                    }

                    //一次性物品费权责
                    HashMap<String, Object> map = new HashMap<>();
                    map.put("memberId", stuId);
                    map.put("paymentId", payment);
                    boolean d = tssRightStatisticService.saveOrUpdateTssRightStatisticService(null, map, adminUserId, 4, 8,
                            0, amount2);
                    if (!d) {
                        System.out.println("一次性物品费存储权责失败");
                    }

                }

            }
        }

        if (!"".equals(reg.toString())) {
            return reg;
        }

        return reg.append(" 导入成功! \n");
    }
}
